package com.app.android.coliy.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.app.android.coliy.R;
import com.app.android.coliy.bean.LocalFileInfo;
import com.app.android.coliy.dialog.Comm_Dialog;
import com.app.android.coliy.event.Event_CurrentFile;
import com.app.android.coliy.interface_.CommCallBack;
import com.app.android.coliy.tool.Log;
import com.app.android.coliy.ui.activity.FileEditActivity;
import com.app.android.coliy.util.BitmapUtil;
import com.app.android.coliy.util.DateUtil;
import com.app.android.coliy.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import static com.app.android.coliy.util.Util.getString;

/**
 * Created by Administrator on 2018/8/22.
 */

public class FileListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context mContext;
    public List<LocalFileInfo> list = null;
    CommCallBack callBack;

    public FileListAdapter(Context mContext, CommCallBack callBack) {
        this.mContext = mContext;
        this.callBack = callBack;
    }

    public void setData(List<LocalFileInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public boolean isMutil;

    public void setIsMutil(boolean isMutil) {
        this.isMutil = isMutil;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        ViewGroup viewGroup = (ViewGroup) mInflater.inflate(R.layout.item_filelist, null, false);
        ContentViewHolder holder = new ContentViewHolder(viewGroup);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContentViewHolder viewHolder = (ContentViewHolder) holder;
        final LocalFileInfo info = list.get(position);
        Log.i("info:" + info.toString());

        viewHolder.tv_name.setText(info.filename);
        try {
            viewHolder.tv_time.setText(DateUtil.getDateFromTimeLine(Long.parseLong(info.timestamp)));
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Bitmap bitmap = BitmapUtil.getSmallBitmap(info.snapshot);
            viewHolder.iv_img.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (info.isSelect) {
            viewHolder.iv_select.setImageResource(R.drawable.ico_selected);
        } else {
            viewHolder.iv_select.setImageResource(R.drawable.ico_unselect);
        }

        viewHolder.iv_delete.setVisibility(View.GONE);
        viewHolder.iv_select.setVisibility(View.GONE);
        viewHolder.iv_print.setVisibility(View.GONE);
        if (isMutil) {
            viewHolder.iv_select.setVisibility(View.VISIBLE);
            //多文件

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    viewHolder.iv_select.performClick();
                }
            });
        } else {
            viewHolder.iv_delete.setVisibility(View.VISIBLE);
            viewHolder.iv_print.setVisibility(View.VISIBLE);

            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext, FileEditActivity.class);
                    intent.putExtra("fileName", info.filename);
                    mContext.startActivity(intent);
                    ((Activity) mContext).finish();
                }
            });
        }

        viewHolder.iv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Comm_Dialog.showCommDialog(mContext, "", getString(R.string.file_list_deletedialo_title), getString(R.string.comm_ok), getString(R.string.comm_cancel), new CommCallBack() {
                    @Override
                    public void onResult(Object obj) {
                        info.delete();
                        if (callBack != null) {
                            callBack.onResult("refresh");
                        }
                    }
                }, null);
            }
        });

        viewHolder.iv_print.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Event_CurrentFile eventCurrentFile = new Event_CurrentFile();
                eventCurrentFile.fileName = info.filename;
                EventBus.getDefault().post(eventCurrentFile);
                ((Activity) mContext).finish();
            }
        });
        viewHolder.iv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!info.isSelect) {
                    int count = 0;
                    for (LocalFileInfo localFileInfo : list) {
                        if (localFileInfo.isSelect) {
                            count++;
                        }
                    }
                    if(count>=20)
                    {
                        ToastUtil.showWarning(mContext, getString(R.string.file_list_print_multi_max));
                        return;
                    }
                }

                info.isSelect = !info.isSelect;
                if (info.isSelect) {
                    viewHolder.iv_select.setImageResource(R.drawable.ico_selected);
                } else {
                    viewHolder.iv_select.setImageResource(R.drawable.ico_unselect);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name, tv_time;
        ImageView iv_delete, iv_print;
        ImageView iv_img, iv_select;

        public ContentViewHolder(View itemView) {
            super(itemView);
            iv_delete = itemView.findViewById(R.id.iv_delete);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_img = itemView.findViewById(R.id.iv_img);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_print = itemView.findViewById(R.id.iv_print);
            iv_select = itemView.findViewById(R.id.iv_select);
        }
    }
}
